﻿using HlELock.Domain.Entities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace HlELock.Core
{
    /// <summary>
    /// 三方卡片操作辅助类
    /// </summary>
    public class SfHelper
    {
        public static SfHelper Instance { get; set; } = new SfHelper();
        private MfLock _lockApi;
        public SfHelper()
        {
            _lockApi = new MfLock(".\\sf");
        }

        /// <summary>
        /// 更新房间信息
        /// </summary>
        public void UpdateRoom(List<Room> rooms)
        {
            string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=.\sf\ic2000.mdb;Jet OLEDB:Database Password=a123;";

            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {

                connection.Open();

                using (OleDbCommand command = new OleDbCommand("DELETE FROM room", connection))
                {
                    var rows = command.ExecuteNonQuery();
                    // 查询数据
                    string query = "INSERT INTO room (building_id ,room_no_id, floor,room_no) VALUES (0,{1},{0},{1});";
                    foreach (var item in rooms)
                    {
                        var exeQuery = string.Format(query, item.FloorNumber, item.RoomNumber);
                        command.CommandText = exeQuery;
                        command.ExecuteNonQuery();
                    }
                }
            }
        }

        /// <summary>
        /// 发客户人卡
        /// </summary>
        /// <param name="roomNumber"></param>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>

        public string IssueGuestCard(string roomNumber, DateTime beginTime, DateTime endTime)
        {
            if (_lockApi.InitConnect())
            {
                var cardNum = _lockApi.GetCardNo();

                var result = _lockApi.IssueGuestCard(roomNumber, "", "0", "0", beginTime.ToString("yyMMddHHss"), endTime.ToString("yyMMddHHss"), cardNum.CardNo);
                if (result != CardOperationResult.成功)
                {
                    _lockApi.Beep(2);
                    throw new Exception(result.ToString());
                }
                else
                {
                    _lockApi.Beep(1);
                }
                return cardNum.CardNo;
            }
            else
            {
                throw new Exception("发卡器初始化失败");
            }
        }


        public CardDetailInfo ReadCardInfo()
        {
            var result = new CardDetailInfo();
            result.CardType = CardType.客人卡;
            if (_lockApi.InitConnect())
            {
                var card = _lockApi.ReadGuestCard();
                if (card.Result != CardOperationResult.成功)
                {
                    _lockApi.Beep(2);

                    MessageBox.Show(card.Result.ToString());
                    return null;
                }
                else
                {
                    _lockApi.Beep(1);
                    result.RoomNumber = card.Data.Room;
                    result.CardId = card.Data.CardNum;
                    result.BeginTime = card.Data.BeginTime;
                    result.EndTime = card.Data.EndTime;
                }
            }
            else
            {
                throw new Exception("发卡器初始化失败");
            }

            return result;
        }

        public string CancelGuestCard()
        {
            if (_lockApi.InitConnect())
            {
                var result = _lockApi.CancelGuestCard();
                if (result == CardOperationResult.成功)
                {
                    _lockApi.Beep(1);
                    MessageBox.Show("房卡注销成功");

                    var (_, CardNo) = _lockApi.GetCardNo();
                    return CardNo;
                }
                else
                {
                    _lockApi.Beep(2);
                    MessageBox.Show(result.ToString());
                    return null;
                }
            }
            else
            {
                throw new Exception("发卡器初始化失败");
            }
        }
    }
}
